package dao

import (
	"errors"
	__ "goods_srv/basic/goodsrpc"
	"goods_srv/handler/models"
	"time"
)

// 添加快递柜
func CreateCabinet(in *__.CreateCabinetReq) (uint32, error) {
	c := models.Cabinet{
		CabinetNo:        in.CabinetNo,
		ChargingStandard: in.ChargingStandard,
		AddressId:        in.AddressId,
		SizeId:           in.SizeId,
		CreatedAt:        time.Now(),
		UpdatedAt:        time.Now(),
	}
	if err := c.AddCabinet(); err != nil {
		return 0, errors.New("添加快递柜失败" + err.Error())
	}
	return c.Id, nil
}

// 修改快递柜
func UpdateCabinet(in *__.UpdateCabinetReq) (bool, error) {
	c := models.Cabinet{
		Id:               in.Id,
		CabinetNo:        in.CabinetNo,
		ChargingStandard: in.ChargingStandard,
		AddressId:        in.AddressId,
		SizeId:           in.SizeId,
		Status:           int8(in.Status),
	}
	if err := c.UpdateCabinet(); err != nil {
		return false, errors.New("更新快递柜失败" + err.Error())
	}
	return true, nil
}

// 快递柜详情
func CabinetDetail(id uint32) (*models.Cabinet, error) {
	var c models.Cabinet
	c.Id = id
	if err := c.CabinetDetail(); err != nil {
		return nil, errors.New("获取快递柜详情失败" + err.Error())
	}
	return &c, nil
}

// 快递柜位置
func CabinetLocation() ([]*__.CabinetLocation, error) {
	var a models.CabinetAddress
	list, err := a.AddressList()
	if err != nil {
		return nil, nil
	}
	var addressList []*__.CabinetLocation
	for _, v := range list {
		l := __.CabinetLocation{
			Id:      int32(v.Id),
			Address: v.Address,
		}
		addressList = append(addressList, &l)
	}
	return addressList, nil
}

// 通过型号查询快递柜
func GetCabinetBySize(in *__.GetCabinetBySizeReq) ([]*__.CabinetList, error) {
	var c models.Cabinet
	c.AddressId = in.AddressId
	c.SizeId = in.SizeId
	list, err := c.GetByAddressAndSize()
	if err != nil {
		return nil, errors.New("查询失败")
	}
	var cabinetList []*__.CabinetList
	for _, v := range list {
		l := __.CabinetList{
			Id:               v.Id,
			CabinetNo:        v.CabinetNo,
			ChargingStandard: v.ChargingStandard,
			AddressId:        v.AddressId,
			SizeId:           v.SizeId,
			Hot:              v.Hot,
			Status:           int32(v.Status),
		}
		cabinetList = append(cabinetList, &l)
	}
	return cabinetList, nil
}
